package com.blink.curator.recipe.tool;

import com.blink.base.BaseTestSample;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.ZooKeeper;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * <ClassName>ZKPathsSample</ClassName>
 * <Description></Description>
 *
 * @Author liuxianzhao
 * @Date 2018年01月14日 11:58
 */
public class ZKPathsSample extends BaseTestSample {
    static String path = "/curator_zkpath_sample";


    @Test
    public void testZKPaths() throws Exception {
        CuratorFramework client = getZkCuratorWithoutNamespace();
        client.start();
        ZooKeeper zookeeper = client.getZookeeperClient().getZooKeeper();
        logger.info(ZKPaths.fixForNamespace(path, "/sub"));
        logger.info(ZKPaths.makePath(path, "sub"));
        logger.info(ZKPaths.getNodeFromPath("/curator_zkpath_sample/sub1"));

        ZKPaths.PathAndNode pn = ZKPaths.getPathAndNode("/curator_zkpath_sample/sub1");
        logger.info(pn.getPath());
        logger.info(pn.getNode());

        String dir1 = path + "/child1";
        String dir2 = path + "/child2";
        ZKPaths.mkdirs(zookeeper, dir1);
        ZKPaths.mkdirs(zookeeper, dir2);
        List<String> sortedChildren = ZKPaths.getSortedChildren(zookeeper, path);
        logger.info(sortedChildren == null ? null : sortedChildren.toString());
        ZKPaths.deleteChildren(client.getZookeeperClient().getZooKeeper(), path, true);
    }
}
